// 设置目标时间（这里设置为当前时间加7天）
const targetDate = new Date();
targetDate.setDate(targetDate.getDate() + 7);

function updateCountdown() {
    const now = new Date();
    const difference = targetDate - now;

    // 计算天、小时、分钟、秒
    const days = Math.floor(difference / (1000 * 60 * 60 * 24));
    const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
    const seconds = Math.floor((difference % (1000 * 60)) / 1000);

    // 更新所有倒计时显示
    document.querySelectorAll('.days').forEach(el => el.textContent = days.toString().padStart(2, '0'));
    document.querySelectorAll('.hours').forEach(el => el.textContent = hours.toString().padStart(2, '0'));
    document.querySelectorAll('.minutes').forEach(el => el.textContent = minutes.toString().padStart(2, '0'));
    document.querySelectorAll('.seconds').forEach(el => el.textContent = seconds.toString().padStart(2, '0'));

    // 如果倒计时结束
    if (difference < 0) {
        clearInterval(countdownInterval);
        document.querySelectorAll('.countdown-title').forEach(el => el.textContent = '活动已结束！');
        document.querySelectorAll('.days').forEach(el => el.textContent = '00');
        document.querySelectorAll('.hours').forEach(el => el.textContent = '00');
        document.querySelectorAll('.minutes').forEach(el => el.textContent = '00');
        document.querySelectorAll('.seconds').forEach(el => el.textContent = '00');
    }
}

// 立即执行一次，避免初始延迟
updateCountdown();

// 每秒更新一次
const countdownInterval = setInterval(updateCountdown, 1000);